package com.monster.valid.validated;

import com.monster.valid.annotation.ValidEnum;
import com.monster.valid.utils.ValidCommonUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 枚举校验类
 *
 * @author kuangxz
 */
public class EnumValidator implements ConstraintValidator<ValidEnum, Object> {
    ValidEnum valid;

    @Override
    public void initialize(ValidEnum constraintAnnotation) {
        valid = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (ValidCommonUtils.checkNull(valid.isNull(), valid.name(), valid.message(), value)) {
            return true;
        }
        // 值必须存在这里面
        for (String str: valid.valueEnum()) {
            if (str.equals(String.valueOf(value))) {
                return true;
            }
        }
        return false;
    }
}
